//
//  ModuleSelectView.m
//  ForestPack
//
//  Created by 郑洲 on 2018/7/23.
//  Copyright © 2018年 郑洲. All rights reserved.
//

#import "ModuleSelectView.h"

@interface ModuleSelectView()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) BaseTableView *tableView;
@property (nonatomic, strong) NSArray *dataArray;

@end

@implementation ModuleSelectView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
        self.dataArray = [NSArray array];
        
        self.tableView = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH - ScreenX375(76), ScreenX375(157)) style:UITableViewStyleGrouped];
        self.tableView.center = CGPointMake(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2);
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        self.tableView.clipsToBounds = YES;
        self.tableView.layer.cornerRadius = 5;
        self.tableView.rowHeight = ScreenX375(59);
        self.tableView.backgroundColor = [UIColor clearColor];
        [self addSubview:self.tableView];
    }
    return self;
}

- (void)modelArrayForView:(NSArray *)array {
    _dataArray = array;
    [_tableView reloadData];
    _tableView.frame = CGRectMake(0, 0, SCREEN_WIDTH - ScreenX375(76), (ScreenX375(39) + _dataArray.count * ScreenX375(59)) > (SCREEN_HEIGHT - NAVIGATION_BAR_HEIGHT - TAB_BAR_HEIGHT) ? (SCREEN_HEIGHT - NAVIGATION_BAR_HEIGHT - TAB_BAR_HEIGHT) : (ScreenX375(39) + _dataArray.count * ScreenX375(59)));
    self.tableView.center = CGPointMake(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2);
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ModuleTableCell *cell = [ModuleTableCell cellWithTableView:tableView andIndexPath:indexPath];
    [cell initWithDict:_dataArray[indexPath.row]];
    if (_isSelectFactory) {
        if ([kNSUDefaultReadKey(@"Factory") isEqualToString:_dataArray[indexPath.row][@"name"]]) {
            [cell selectModule:YES];
        }else {
            [cell selectModule:NO];
        }
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (_isSelectFactory) {
        kNSUDefaultSaveVauleAndKey(_dataArray[indexPath.row][@"name"], @"Factory");
        kNSUDefaultSaveVauleAndKey(_dataArray[indexPath.row][@"id"], @"FactoryId");
        [_tableView reloadData];
        if (_selectClick) {
            self.selectClick();
        }
    }else {
        if (_chooseClick) {
            self.chooseClick(indexPath.row);
        }
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return ScreenX375(27);
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return ScreenX375(12);
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor whiteColor];
    return view;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor whiteColor];
    return view;
}

@end

@interface ModuleTableCell ()

@property (nonatomic, strong) UIButton *moduleBtn;

@end

@implementation ModuleTableCell

+ (ModuleTableCell *)cellWithTableView:(UITableView *)tableView andIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellID = @"ModuleTableCell";
    ModuleTableCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[ModuleTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor whiteColor];
        [cell createSubView];
    }
    
    return cell;
}

- (void)createSubView {
    self.moduleBtn = [[UIButton alloc] initWithFrame:CGRectMake((SCREEN_WIDTH - ScreenX375(242)) / 2, 0, ScreenX375(166), ScreenX375(44))];
    [self.moduleBtn setTitleColor:FONTCOLOR_LIGHTGRAY forState:UIControlStateNormal];
    [self.moduleBtn setTitleColor:ThemeColor forState:UIControlStateSelected];
    self.moduleBtn.titleLabel.font = Kfont(16);
    self.moduleBtn.layer.cornerRadius = ScreenX375(22);
    self.moduleBtn.clipsToBounds = YES;
    self.moduleBtn.layer.borderWidth = 1;
    self.moduleBtn.userInteractionEnabled = NO;
    [self addSubview:self.moduleBtn];
}

- (void)initWithDict:(NSDictionary *)dict {
    [self.moduleBtn setTitle:dict[@"name"] forState:UIControlStateNormal];
}

- (void)selectModule:(BOOL)isSelect {
    self.moduleBtn.selected = isSelect;
    if (isSelect) {
        self.moduleBtn.layer.borderColor = ThemeColor.CGColor;
    }else {
        self.moduleBtn.layer.borderColor = LineColor.CGColor;
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    // Configure the view for the selected state
}

@end
